package com.example.communitypro.controller;

import com.example.communitypro.pojo.CommunityInformation;
import com.example.communitypro.pojo.Notification;
import com.example.communitypro.pojo.Post;
import com.example.communitypro.service.NotificationService;
import com.example.communitypro.service.PostService;
import com.example.communitypro.service.UserCollectionService;
import com.example.communitypro.util.result.NotificationResult;
import com.example.communitypro.util.result.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "通知相关接口")
@RestController
@RequestMapping("/notification")
@CrossOrigin
public class NotificationController {
    @Autowired
    private NotificationService notificationService;
    @Autowired
    private UserCollectionService userCollectionService;
    @Autowired
    private PostService postService;

    @ApiOperation("根据id删除通知接口")
    @ApiImplicitParam(name = "notification_id",value = "需要删除的通知主键id")
    @GetMapping(value = "/dropNotification")
    public Result dropNotification( Integer notification_id) {
        return notificationService.dropNotification(notification_id);
    }

    @ApiOperation("修改通知接口")
    @ApiImplicitParam(name = "record",value = "通知实体类")
    @PostMapping(value = "/modifyNotification")
    public Result modifyNotification(Notification record) {
        return notificationService.modifyNotification(record);
    }

    @ApiOperation("根据id查询通知接口")
    @ApiImplicitParam(name = "notification_id",value = "需要查询的id")
    @GetMapping(value = "/queryNotification")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回通知id查询的通知",response = NotificationResult.class)
    })
    public Result queryNotification(int notification_id) {
        return notificationService.queryNotification(notification_id);
    }


    @ApiOperation("查询所有通知")
    @GetMapping(value = "/selectAll")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回所有通知",response = NotificationResult.class)
    })
    public Result selectAll() {
        return notificationService.selectAll();
    }

    @ApiOperation("查询指定用户no的所有通知")
    @ApiImplicitParam(name = "userNo",value = "需要查询的用户no")
    @GetMapping(value = "/selectByUser")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回通过指定userNo查询的所有通知",response = NotificationResult.class)
    })
    public Result selectByUser(Integer userNo) {
        return notificationService.queryNotificationByUserNo(userNo);
    }


    @ApiOperation("根据id修改通知已读")
    @ApiImplicitParam(name = "notificationId",value = "需要设置为已读的通知id")
    @PostMapping(value = "/updateIsReadByUserNo")
    public Result updateIsReadByUserNo(Integer notificationId) {
        return notificationService.updateIsReadByUserNo(notificationId);
    }

    @ApiOperation("查询自己关注社区新发布的帖子")
    @ApiImplicitParam(name = "userNo",value = "用户no")
    @PostMapping(value = "/selectToCollection")
    public Result selectToCollection(Integer userNo) {
        /**
         * lsl
         */
        List<Notification> resNotifications = new ArrayList<>();



        Result result = userCollectionService.queryCommunityCollection(userNo);
        if(result.getData().get("communityInformations")==null){
            return Result.error().message("未关注社区");
        }
        List<CommunityInformation> communityInformations = (List<CommunityInformation>) result.getData().get("communityInformations");
        List<Notification> notifications = notificationService.queryNotificationByType(6);

        for (CommunityInformation communityInformation : communityInformations) {
            for (Notification notification : notifications) {
                if (notification.getPostNo().equals(communityInformation.getCommunityNo())){
                    resNotifications.add(notification);
                }
            }
        }
        return Result.ok().message("查询成功").data("notifications",resNotifications);
    }

}
